﻿////////////////////////////////////////////////////////////////////////
//                                                                    //
//                 Project: MediaElement Extensions                   //
//               Project URL: http://mext.codeplex.com                //
//                           GPL License v2                           //
//                                                                    //
//             Written by Geoffrey DANIEL { NeuroCypher }             //
//           Blog: http://blogs.developpeur.org/neurocypher           //
//                                                                    //
////////////////////////////////////////////////////////////////////////

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MediaElementExtensions.Controls.Bases;

namespace MediaElementExtensions.Controls
{

    public class StopButton : ButtonMediaElementExtensionControlBase
    {

        #region DependencyProperties

        public static DependencyProperty AutoDisableWhenStoppedProperty =
            DependencyProperty.Register("AutoDisableWhenStopped",
                                        typeof(bool),
                                        typeof(StopButton),
                                        null);

        public bool AutoDisableWhenStopped
        {
            get { return (bool)GetValue(AutoDisableWhenStoppedProperty); }
            set { SetValue(AutoDisableWhenStoppedProperty, value); }
        }

        #region CornerRadius

        public static DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(StopButton), null);

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        #endregion

        #region Fill Brush

        public static DependencyProperty FillBrushProperty =
            DependencyProperty.Register("FillBrush", typeof(Brush), typeof(StopButton), null);

        public Brush FillBrush
        {
            get { return (Brush)GetValue(FillBrushProperty); }
            set { SetValue(FillBrushProperty, value); }
        }

        #endregion

        #endregion

        #region Constructor

        public StopButton()
        {

            Click += StopMedia;

            InitializeActions(
                CheckIsEnabled,
                (o, e) => CheckIsEnabled(),
                null,
                null,
                (o, e) => CheckIsEnabled(),
                null);

            CheckIsEnabled();

            DefaultStyleKey = typeof(StopButton);

        }

        #endregion

        #region CheckIsEnabled

        public override void CheckIsEnabled()
        {

            if (AutoDisableWhenNoSourceLoaded == true &&
                (MediaElement == null || MediaElement.CurrentState == MediaElementState.Closed))
            {
                IsEnabled = false;
            }
            else if (AutoDisableWhenStopped == true && MediaElement.CurrentState == MediaElementState.Stopped)
            {
                IsEnabled = false;
            }
            else
            {
                IsEnabled = true;
            }

        }

        #endregion

        #region Action

        public void StopMedia(object sender, RoutedEventArgs e)
        {

            if (MediaElement == null || MediaElement.CurrentState == MediaElementState.Closed)
            {
                CheckIsEnabled();
            }
            else if (MediaElement.CurrentState == MediaElementState.Stopped)
            {
                if (AutoDisableWhenStopped == true)
                {
                    CheckIsEnabled();
                }
            }
            else
            {
                MediaElement.Stop();
            }

        }

        #endregion

    }

}
